<template>
  <view class="content">
    <view class="jtop">
      <view class="status_bar"><!-- 这里是状态栏 --></view>
      <view class="jtop_l" @tap="navigateBack"
        ><image src="../../static/img/ic_back.png"></image
      ></view>
      <view class="jtop_c">创建企业/团队</view>
    </view>
    <view class="tophr"></view>

    <view class="input">
      <view class="input_l">
        企业全称
      </view>
      <input
        type="text"
        placeholder="请输入企业全称"
        class="input_c"
        v-model="fullName"
        focus
      />
    </view>

    <view class="input">
      <view class="input_l">
        企业简称
      </view>
      <input
        type="text"
        placeholder="请输入企业简称"
        class="input_c"
        v-model="shortName"
      />
    </view>

    <view class="addi">
      <text>企业logo</text>
      <view class="att" @tap="jshow_pop">
        <image :src="tpurl" mode="" class="at"></image>
      </view>
    </view>

    <view class="btn-row">
      <view
        type="primary"
        class="bt primary"
        @tap="bindLogin"
        :loading="isLoading"
        >提交</view
      >
    </view>
  </view>
</template>

<script>
import request from '@/common/request.js';
import service from '../../service.js';
import mInput from '../../components/m-input.vue';
import {
  pathToBase64,
  base64ToPath,
} from '../../components/image-tools/index.js';
export default {
  components: {
    mInput,
  },
  data() {
    return {
      repeat_click: true,
      providerList: [],
      hasProvider: false,
      account: '',
      yzm: '',
      password: '',
      verifyCode: '',
      positionTop: 0,
      isDevtools: false,
      seconds: 60,
      disableCodeBtn: true,
      codeBtn: {
        text: '获取验证码',
        waitingCode: false,
        count: this.seconds,
      },
      isLoading: false,
      canclick: false,
      mparameterw: false,
      tpurl: '../../static/img/index/new/at.png',
      fullName: '',
      shortName: '',
      tpurl2: '',
    };
  },
  methods: {
    jshow_pop() {
      var that = this;
      uni.chooseImage({
        count: 1, //默认9
        sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
        sourceType: ['album'], //从相册选择、摄像头
        success: function(res) {
          console.log(JSON.stringify(res.tempFilePaths, res.tempFiles[0]));
          that.sctp(res.tempFilePaths, res.tempFiles[0]);
        },
      });
    },

    //上传图片
    sctp(a, b) {
      console.log(a);

      var als = '';
      var tpbt = b.name;
      if (tpbt == undefined || tpbt == 'undefined') {
        tpbt = b.path;
      }
      var pos = tpbt.lastIndexOf('.');
      var suffix = tpbt.substring(pos);
      function random_string(len) {
        len = len || 32;
        var chars = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678';
        var maxPos = chars.length;
        var pwd = '';
        for (var i = 0; i < len; i++) {
          pwd += chars.charAt(Math.floor(Math.random() * maxPos));
        }
        return pwd;
      }
      var g_object_name = random_string(10) + suffix;

      // 获取信息
      request({
        url: '/biz/oss/policy',
        method: 'GET',
        success: res => {
          if (res.data.success == true) {
            console.log('22222');

            als = res.data.data;
            request({
              filePath: a[0],
              oss: true,
              formData: {
                name: tpbt,
                key: als.dir + g_object_name,
                policy: als.policy,
                OSSAccessKeyId: als.accessid,
                signature: als.signature,
                callback: als.callback,
                success_action_status: '200',
              },

              success: res => {
                var cc = JSON.parse(res.data);
                console.log('上传图片', cc);
                if (cc.code == '00000') {
                  // var lstp = cc.data.filename;
                  // var lstp2 = cc.data.url;
                  this.tpurl = cc.data.url + '/' + als.dir + g_object_name;
                  this.tpurl2 = g_object_name;
                } else {
                  this.repeat_click = true;
                  uni.showToast({
                    title: cc.message,
                    icon: 'none',
                  });
                }
              },
            });
          }
        },
      });
    },

    navigateBack() {
      // uni.navigateTo({
      // 	url: '../user/user_select',
      // });
      uni.navigateBack();
    },

    // 验证
    bindLogin() {
      if (this.fullName.length < 1) {
        this.repeat_click = true;
        uni.showToast({
          icon: 'none',
          title: '请填写企业全称',
        });
        return;
      }

      // if (this.shortName.length < 1) {
      // 						this.repeat_click = true;
      //uni.showToast({
      // 		icon: 'none',
      // 		title: '请填写企业简称'
      // 	});
      // 	return;
      // }

      // if (this.tpurl == "../../static/img/index/new/at.png") {
      // 						this.repeat_click = true;
      //uni.showToast({
      // 		icon: 'none',
      // 		title: '请上传企业logo'
      // 	});
      // 	return;
      // }

      request({
        url: '/ucenter/corp-base-info',
        data: {
          fullName: this.fullName,
          shortName: this.shortName,
          // logo: "https://www.mlito.com/wp-content/uploads/e824cf92258523.5e46849db693a.jpg",
          logo: this.tpurl2,
        },

        method: 'POST',
        success: openIdRes => {
          console.log(openIdRes);

          // 已经授权了、查询到用户的数据了
          if (openIdRes.data.success == true) {
            wx.showToast({
              title: '创建成功',
              icon: 'none',
              duration: 1500,
              success: function() {
                setTimeout(function() {
                  uni.switchTab({
                    url: '/pages/main/main',
                  });
                }, 1500);
              },
            });
          } else {
            // 验证码错误
            this.repeat_click = true;
            uni.showToast({
              title: openIdRes.data.msg,
              icon: 'none',
            });
            this.isLoading = false;
          }
        },
        fail: () => {
          this.repeat_click = true;
          uni.showToast({
            title: '服务器开小差，请稍后~',
            icon: 'none',
          });
          return false;
        },
      });
    },
  },
};
</script>

<style>
.addi {
  width: 100%;
  height: auto;
  display: block;
  float: left;
  background: #ffffff;
  box-shadow: inset 0px 1px 0px #dddddd, inset 0px -1px 0px #dddddd;
  margin: 20rpx 0 0;
}
.addi text {
  width: 200rpx;
  height: 90rpx;
  display: block;
  float: left;
  line-height: 90rpx;
  font-size: 34rpx;
  color: #000000;
  margin: 0 0 0 30rpx;
}

.att {
  width: 720rpx;
  margin: 0 0 30rpx 30rpx;
  height: auto;
  display: block;
  float: left;
  background: #ffffff;
}
.at {
  width: 160rpx;
  height: 160rpx;
  display: block;
  float: left;
  margin: 0rpx 0 0 0;
}

.jtop {
  width: 100%;
  height: 88rpx;
  background: #ffffff;
  position: fixed;
  top: 0;
  left: 0;
  z-index: 1000;
  box-shadow: inset 0px -1px 0px #dddddd;
}
.jtop_l {
  width: auto;
  height: 88rpx;
  display: block;
  float: left;
  font-size: 32rpx;
  color: #000000;
  line-height: 88rpx;
  margin: 0 0 0 30rpx;
}
.jtop_l image {
  width: 48rpx;
  height: 48rpx;
  display: block;
  float: left;
  margin: 20rpx 0 20rpx 0;
}
.jtop_lt {
  margin: 0 0 0 10rpx;
}
.jtop_r {
  width: 48rpx;
  height: 48rpx;
  display: block;
  float: right;
  margin: 20rpx 20rpx 0 0;
}
.jtop_rl {
  margin: 20rpx 30rpx 0 0;
}
.jtop_r image {
  width: 48rpx;
  height: 48rpx;
  display: block;
  float: left;
}
.jtop_c {
  width: 70%;
  height: 48rpx;
  line-height: 48rpx;
  display: block;
  float: left;
  position: fixed;
  top: 20rpx;
  left: 15%;
  text-align: center;
  font-size: 36rpx;
  color: #000000;
}
.jtop_c image {
  width: 36rpx;
  height: 36rpx;
  display: inline-block;
  float: none;
  margin-bottom: -5rpx;
  margin-left: 5rpx;
}
.jtop_b {
  width: 100%;
  height: auto;
  background: #ffffff;
  position: fixed;
  top: 88rpx;
  left: 0;
  z-index: 1001;
}
.tophr {
  width: 100%;
  height: 88rpx;
}
.content {
  background: #f2f2f2;
}
.top {
  width: 100%;
  height: 36rpx;
  display: block;
  float: left;
  line-height: 36rpx;
  font-size: 28rpx;
  color: #888888;
  padding: 30rpx 0 10rpx 30rpx;
  border-bottom: 1px solid #dddddd;
}
.input {
  width: 100%;
  height: auto;
  display: block;
  float: left;
  background: #ffffff;
  border-bottom: 1px solid #dddddd;
}
.input_l {
  width: 166rpx;
  height: 42rpx;
  display: block;
  float: left;
  margin: 24rpx 10rpx 24rpx 30rpx;
  font-size: 34rpx;
  color: #000000;
  line-height: 42rpx;
}
.input_c {
  width: 330rpx;
  height: 42rpx;
  display: block;
  float: left;
  margin: 24rpx 10rpx 0 0rpx;
  font-size: 32rpx;
  color: #000000;
  line-height: 42rpx;
}
.input_r {
  width: auto;
  height: 42rpx;
  display: block;
  float: right;
  margin: 24rpx 30rpx 0 0rpx;
  font-size: 32rpx;
  color: #256aff;
  line-height: 42rpx;
}
.bt {
  width: 690rpx;
  height: 94rpx;
  margin: 30rpx 0 0 30rpx;
  background: #256aff;
  border-radius: 10rpx;
  text-align: center;
  line-height: 94rpx;
  font-size: 36rpx;
  color: #ffffff;
  display: block;
  float: left;
  border: 0;
}
.bt:active {
  background: linear-gradient(
      0deg,
      rgba(0, 0, 0, 0.199437),
      rgba(0, 0, 0, 0.199437)
    ),
    #256aff;
  border-radius: 8px;
}

.not uni-button {
  opacity: 0.5;
}

.input_r {
  width: auto;
  height: 42rpx;
  display: block;
  float: right;
  margin: 24rpx 30rpx 40rpx 0rpx;
  font-size: 32rpx;
  color: #256aff;
  line-height: 42rpx;
  background: none !important;
  border: 0 !important;
  padding: 0;
}
uni-button:after {
  border: 0 !important;
}

.input_r:active {
  color: #256aff;
  background: none !important;
}

.input_r.active {
  color: rgb(176, 173, 176);
}
</style>
